
import java.util.Random;
import mhv.fr.sprite.Sprite;
import mhv.fr.sprite.controller.SpriteController;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Marc-Henri
 */
public class SpiderController extends SpriteController {

    /**
     *
     */
    public enum SpiderStatus {

        RIGHT, LEFT, UP, DOWN
    };
    final int SPRITE_SHIP = 1;
    final int SPRITE_BULLET = 2;
    final int SPRITE_MUSHROOM_NORMAL = 10;
    final int SPRITE_MUSHROOM_HIT_1 = 11;
    final int SPRITE_MUSHROOM_HIT_2 = 12;
    final int SPRITE_CENTIPEDE_HEAD = 20;
    final int SPRITE_CENTIPED_BODY = 21;
    //private double nextX;
    private double nextY;
    private SpiderStatus hStatus;
    private SpiderStatus vStatus;
    private Random rand;
    static int SPIDERSIZE = 10;
    static int SPIDERMAXHEIGHT = 590;
    static int SPIDERMINHEIGHT = 450;
    static double SPIDERSPEED = 1.0;
    static int SCREENWIDTH = 800;
    static int SCREENHEIGHT = 600;

    @Override
    public void move() {
        if (nextY == getGameEntity().getY()) { //calculate next point
            double spiderSpeed = (rand.nextDouble() + 1.5);
            int dice = rand.nextInt(100);

            if (vStatus == SpiderStatus.UP) {
                vStatus = SpiderStatus.DOWN;
                if (dice < 30) {
                    nextY = SPIDERMINHEIGHT + 20;
                } else {
                    nextY = SPIDERMINHEIGHT;
                }
                getGameEntity().setVelY(-spiderSpeed);
            } else {
                vStatus = SpiderStatus.UP;
                if (dice < 30) {
                    nextY = SPIDERMAXHEIGHT - 20;
                } else {
                    nextY = SPIDERMAXHEIGHT;
                }
                getGameEntity().setVelY(spiderSpeed);
            }

            dice = rand.nextInt(100);
            if (dice < 35) {
                getGameEntity().setVelX(0);
            } else {
                if (hStatus == SpiderStatus.RIGHT) {
                    getGameEntity().setVelX(SPIDERSPEED + rand.nextDouble());
                } else {
                    getGameEntity().setVelX(-(SPIDERSPEED + rand.nextDouble()));
                }
            }

        } else {
            //move spider
            if ((Math.abs(getGameEntity().getY() - nextY)) < Math.abs(getGameEntity().getVelY())) {
                getGameEntity().setY(nextY);
            } else {
                getGameEntity().incY(getGameEntity().getVelY());
            }
            getGameEntity().incX(getGameEntity().getVelX());


            //chech if spider out of screen
            if (getGameEntity().isAlive() && (getGameEntity().getX() > SCREENWIDTH + SPIDERSIZE) && (hStatus == SpiderStatus.RIGHT)) {
                getGameEntity().setAlive(false);
            }

            if (getGameEntity().isAlive() && (getGameEntity().getX() < -SPIDERSIZE) && (hStatus == SpiderStatus.LEFT)) {
                getGameEntity().setAlive(false);
            }
        }//else
    }//else

    @Override
    public void init() {
        int dice = rand.nextInt(100);
        //set the spider direction
        if (dice < 50) {
            hStatus = SpiderStatus.RIGHT;
            getGameEntity().setX(0 - SPIDERSIZE);
        } else {
            hStatus = SpiderStatus.LEFT;
            getGameEntity().setX(SCREENWIDTH + SPIDERSIZE);
        }
        vStatus = SpiderStatus.UP;
        getGameEntity().setY((SPIDERMAXHEIGHT + SPIDERMINHEIGHT) / 2);
        //nextX = getGameEntity().getX();
        nextY = getGameEntity().getY();
    }

    @Override
    public void doCollide(Sprite sp) {
        switch (sp.getType()) {
            case SPRITE_BULLET:
                getGameEntity().setAlive(false);
                break;
        }
    }

    public SpiderController() {
        super();
        rand = new Random();
    }
}
